package com.cap.bts.crmdp.dealer.remind.scheduler;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.cap.bts.crmdp.commons.sftp.SFTPProcessor;
import com.cap.bts.crmdp.commons.sftp.SftpProperties;
import com.cap.bts.crmdp.dealer.dealerinfosync.util.ProcessCsvFileUtils;
import com.cap.bts.crmdp.dealer.purchase.constants.PurchaseConstants;
import com.cap.bts.crmdp.dealer.remind.dto.SftpFileDto;
import com.cap.bts.crmdp.dealer.remind.service.IRemindService;
import com.cap.bts.crmdp.dealer.remind.service.impl.RemindServiceImpl;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.ApplicationContextUtil;
import com.cap.bts.framework.scheduler.service.BaseJobBean;
import com.jcraft.jsch.ChannelSftp;

public class CustomerAndVehicleFileSynchronizationJob extends BaseJobBean {
	private static final Logger LOGGER = Logger.getLogger(RemindServiceImpl.class);
	@Override
	public void work() throws Exception {
		processCustomerAndVehicle();
	}
	
	public void processCustomerAndVehicle() throws AppException{
		IRemindService remindService = (IRemindService) ApplicationContextUtil.getBean("remindServiceImpl");
		ICodeMapping codeMappingService = (ICodeMapping) ApplicationContextUtil.getBean("codeMappingImpl");
		//从SFTP上下载文件
		String sftpInfo = codeMappingService.getCodeValueByCode(PurchaseConstants.SFTP_INFO, PurchaseConstants.SERVICE_REMIND_FILE_SYNCHRONIZATION);
		try {
			if(sftpInfo != null){
				SftpProperties sftpProperties = JSON.parseObject(sftpInfo, SftpProperties.class);
				//取出所有的文件
				String prefix = codeMappingService.getCodeValueByCode("SRFromMARS", "SRFromMARS");
				List<SftpFileDto> fileList = remindService.getAllFileOrderByDateAsc(sftpProperties, prefix);
				
				SFTPProcessor ftpUtil = new SFTPProcessor();
				ChannelSftp channelSftp = ftpUtil.getConnect(sftpProperties);
				
				if(channelSftp.isConnected()){
					//获取临时目录
					String tmpPath = codeMappingService.getCodeValueByCode("tmpPath", "tmpPath");
					
					for(int j=fileList.size()-1; j>=0; j--){
						try{
							SftpFileDto sftpFileDto = fileList.get(j);
							String localFilePath = tmpPath + "ServiceReminder/CustomerAndVehicle_" + DateFormatUtils.format(new Date(), "yyyyMMdd_HHmmssSSS");
							String localFile = localFilePath + "/" + sftpFileDto.getFileName();
							File file = new File(localFilePath);
							if(!file.exists()){
								file.mkdirs();
							}
							ftpUtil.downloadFile(sftpFileDto.getFileName(), sftpProperties.getPath(), localFile);
							//解压缩
							ProcessCsvFileUtils.unZip(localFile, localFilePath);
							String customerFile = localFilePath + "/Customer.csv";
							remindService.processCustomerFile(customerFile);
							//对下载的文件进行处理  vehicleFile
							String vehicleFile = localFilePath + "/Vehicle.csv";
							remindService.processVehicleFile(vehicleFile);
							
							//将处理过的压缩包删除
							LOGGER.info("要删除的压缩包名称： -----"+sftpProperties.getPath() + sftpFileDto.getFileName());
							ftpUtil.remove(sftpProperties.getPath() + sftpFileDto.getFileName());
						}catch(Exception e){
							LOGGER.error("processCustomerFileError",e);
						}
					}
					
				}
				ftpUtil.closeChannel();
			}
		} catch (RuntimeException e) {
			LOGGER.info("downLodeCustomerVehicleFile", e);
		} catch (Exception e) {
			LOGGER.info("downLodeCustomerVehicleFile", e);
		}
	}
}
